package org.example.service;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.dao.Employee;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class EmployeeService {
    private SqlSessionFactory sqlSessionFactory;
    public EmployeeService(String configPath) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream(configPath);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    public List<Employee> findEmployee() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Employee> list = sqlSession.selectList("findAllEmployee");
        sqlSession.close();
        // 增删改遇到手动提交事务 sqlSession.commit()
        return list;
    }

    public void addEmployee(Employee employee) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        int i = sqlSession.insert("addEmployee", employee);
        System.out.println(i);
        sqlSession.commit();
        sqlSession.close();
    }
}
